/**
 * Created by Administrator on 15-8-3.
 */
//类型切换
var PtypeSwitch = (function(){
	var TYPES = {
		"A" : {
			name : "景区门票",
			levels : ["5A|5","4A|4","3A|3","其它|0"]
		},
		"C" : {
			name : "度假酒店",
			levels : ["5星级|5","4星级|4","3星级|3","其它|0"]
		}
        //B 旅游线路
	};
	var ptypeSelect = null;
	var ptypeTitle = null;
	var ptypeUl = null;
	var ptypeSwitchBtn = null;
	var levelSelect = null;
	var levelTitle = null;
	var levelUl = null;
	var levelSwitchBtn = null;
	var ptype_hidInp = null;
	var level_hidInp = null;
	var ptypeInitItem = null;
	var levelInitItem = null;
    var levelUl_mark = false;
	function init(){
		ptypeSelect = $("#ptypeSelect");
		ptypeTitle = ptypeSelect.find(".province .pro_txt");
		ptypeUl = ptypeSelect.find(".pro_list");
		ptypeSwitchBtn = ptypeSelect.find(".setbg");
		ptype_hidInp = $("#ptype_hidInp");
		levelSelect = $("#levelSelect");
		levelTitle = levelSelect.find(".province .pro_txt");
		levelUl = levelSelect.find(".pro_list");
		levelSwitchBtn = levelSelect.find(".setbg");
		level_hidInp = $("#level_hidInp");
		buildPtype();
		ptypeSwitchBtn.on("click",function(e){
			ptypeUl.toggle();
		})
		levelSwitchBtn.on("click",function(e){
			levelUl.toggle();
		})
		ptypeUl.on("click","li",function(e){
			var tarOption = $(e.currentTarget);
			var name = tarOption.text();
			var type = tarOption.attr("data-ptype").toUpperCase();
			tarOption.addClass("active").siblings().removeClass("active");
			ptypeTitle.text(name);
			ptype_hidInp.val(type);
			ptypeUl.hide();
			buildLevel(type);
		})
		levelUl.on("click","li",function(e){
            if(levelUl_mark==false){
                levelUl_mark = true;
                return;
            }
			var tarOption = $(e.currentTarget);
			var name = tarOption.text();
			var level = tarOption.attr("data-level");
			tarOption.addClass("active").siblings().removeClass("active");
			levelTitle.text(name);
			level_hidInp.val(level);
			levelUl.hide();
		})
		if(ptype_hidInp.val()){
			ptypeInitItem = ptypeUl.children().filter(function(){
				return $(this).attr("data-ptype")==ptype_hidInp.val();
			})[0];
			ptypeInitItem = $(ptypeInitItem);
		}else{
			ptypeInitItem = ptypeUl.children().first();
		}
		if(level_hidInp.val()){
			levelInitItem = levelUl.children().filter(function(){
				return $(this).attr("data-level")==level_hidInp.val();
			})[0];
			levelInitItem = $(levelInitItem);
//			levelInitItem = levelUl.children(["data-level="+level_hidInp.val()])
		}else{
			levelInitItem = levelUl.children().first();
		}
		ptypeInitItem.trigger("click");
//		levelInitItem.trigger("click");
	}
	function buildPtype(){
		var html = "";
		for(var i in TYPES){
			var type = i;
			var name = TYPES[i]["name"];
			html += '<li data-ptype="'+type+'">'+name+'</li>';
		}
		ptypeUl.html(html);
	}
	function buildLevel(type){
		var html = "";
		var levels = TYPES[type]["levels"];
        var name = $("#level_hidInp").val();
		for(var i in levels){
			var json = levels[i].split("|");
			var level = json[1];
			var levelName = json[0];
            if(level==name)
            levelTitle.text(levelName);
			html += '<li data-level="'+level+'">'+levelName+'</li>';
		}
		levelUl.html(html);
		setTimeout(function(){
			levelUl.children().first().trigger("click");
		},10)
	}


	return {init:init}


})();